#define MAX_WEP_PICKUP	210
#define MAX_SPAWNS      50
/*
	LV
	SF
	LS
	DESERT
	COUNTRY
*/
forward RespawnPickup(id);
forward DeathmatchSpawn(playerid);
enum PickupEnum
{
	ID,
	wType,
	Float:wPosX,
	Float:wPosY,
	Float:wPosZ
}
new
	AreaNames[5][4]=
	{
	    "LV",
	    "SF",
	    "LS",
	    "DES",
	    "CNT"
	},
	WepPickupData[MAX_WEP_PICKUP][PickupEnum],
	Float:playerSpawns[MAX_SPAWNS][4],
	Text:FreeDMScore,
	TotalSpawns;

enum FreeDM_pData
{
	pKills,
	pDeaths,
	pHeadshots
}
new FreeDM_pInfo[MAX_PLAYERS][FreeDM_pData];

ActivateFreeDM(area)
{
	new r, FDM_File[40], File:FDM_dFile, line[128], tmpID;
	PlayerLoop(i)
	{
		pDM_Data[i][Team]=i+10;
		GivePlayerWeapon(i, 24, 7*5);
		LoadFreeDMStats(i);
		bitFalse(bPlayerGameSettings[i], GodMode);
	}


	// Weapons


	format(FDM_File, 40, FDM_w, AreaNames[area]);

	if(!fexist(FDM_File))printf(" -ERROR: FILE '%s' NOT FOUND", FDM_File);
	FDM_dFile=fopen(FDM_File, io_read);
	while(fread(FDM_dFile, line))
	{
		r=22+random(12);
		sscanf(line, "p<,>fff", WepPickupData[tmpID][wPosX], WepPickupData[tmpID][wPosY], WepPickupData[tmpID][wPosZ]);
		WepPickupData[tmpID][ID]=CreateDynamicPickup(WepData[r][WepModel], 1, WepPickupData[tmpID][wPosX], WepPickupData[tmpID][wPosY], WepPickupData[tmpID][wPosZ]);
		WepPickupData[tmpID][wType]=r,
		tmpID++;
	}
	fclose(FDM_dFile);


	// SpawnPoints


	tmpID=0;
	format(FDM_File, 40, FDM_s, AreaNames[area]);

	if(!fexist(FDM_File))printf(" -ERROR: FILE '%s' NOT FOUND", FDM_File);
	FDM_dFile=fopen(FDM_File, io_read);
	while(fread(FDM_dFile, line))
	{
	    sscanf(line, "p<,>ffff", playerSpawns[tmpID][0], playerSpawns[tmpID][1], playerSpawns[tmpID][2], playerSpawns[tmpID][3]);
	    tmpID++;
	}
	TotalSpawns=tmpID;
	fclose(FDM_dFile);


	msga(LGREEN, "Free For All Deathmatch mode {FFFF00}is activted!");
	msga(YELLOW, "Fight for yourself or team up, but watch out for traitors!");
	bitTrue(bServerGlobalSettings, FreeDM);
	if(!fexist(FreeDMFile))file_Create(FreeDMFile);
	SetGameModeText("Freeroam Deathmatch");
	
	FreeDM_ScoreUpdate();
	return 1;
}
DeactivateFreeDM()
{
    PlayerLoop(i)
	{
		pDM_Data[i][Team]=FreeID;
		ResetPlayerWeapons(i);
		SaveFreeDMStats(i);
	}
	for(new w;w<MAX_WEP_PICKUP;w++)
	{
	    if(IsValidDynamicPickup(WepPickupData[w][ID]))
	    {
			DestroyDynamicPickup(WepPickupData[w][ID]);
			WepPickupData[w][wType]=0,
			WepPickupData[w][wPosX]=0.0,
			WepPickupData[w][wPosY]=0.0,
			WepPickupData[w][wPosZ]=0.0;
		}
	}
	msga(LGREEN, "Free For All Deathmatch mode is deactivted");
    bitFalse(bServerGlobalSettings, FreeDM);
	SetGameModeText("Freeroam[NoDM]");
	TextDrawHideForAll(FreeDMScore);
	return 1;
}


script_FreeDM_OnPlayerPickup(playerid, pickupid)
{
	for(new w;w<MAX_WEP_PICKUP;w++)
	{
	    if(pickupid==WepPickupData[w][ID])
	    {
	        GivePlayerWeapon(playerid, WepPickupData[w][wType], ((WepData[WepPickupData[w][wType]][MagSize]*(1+random(6)))));
	        DestroyDynamicPickup(WepPickupData[w][ID]);
	        SetTimerEx("RespawnPickup", (1000*60)*random(3), false, "d", w);
	    }
	}
}
public RespawnPickup(id)
{
	WepPickupData[id][ID]=CreateDynamicPickup(WepData[WepPickupData[id][wType]][WepModel], 1, WepPickupData[id][wPosX], WepPickupData[id][wPosY], WepPickupData[id][wPosZ]);
}
script_FreeDM_OnPlayerDeath(playerid, killerid, reason)
{
	new str[128];
	format(str, 128, "**{00FF00}%s {0000FF}was killed by {FF0000}%s {0000FF}with {FFFF00}%s", GetName(playerid), GetName(killerid), WepData[reason][WepName]);
	SendClientMessageToAll(0xFFFF00AA, str);

	GivePlayerMoney(killerid, 50);
	GivePlayerMoney(playerid, -20);
	SetPlayerScore(killerid, GetPlayerScore(killerid)+1);
	
	FreeDM_pInfo[killerid][pKills]++;
	FreeDM_pInfo[playerid][pDeaths]++;
	FreeDM_ScoreUpdate();
}

FreeDM_ScoreUpdate()
{
	new boardStr[256]=0, tmpStr[64];
	strcat(boardStr, "~b~Score Board~n~~n~~g~");

	PlayerLoop(i)
	{
	    format(tmpStr, 64, "%s: %d~n~", GetName(i), FreeDM_pInfo[i][pKills]);
	    strcat(boardStr, tmpStr);
	}
	TextDrawSetString(FreeDMScore, boardStr);
	TextDrawShowForAll(FreeDMScore);
}

public DeathmatchSpawn(playerid)
{
	new r=random(TotalSpawns);

	SetPlayerPos(playerid, playerSpawns[r][0], playerSpawns[r][1], playerSpawns[r][2]);
	SetPlayerFacingAngle(playerid, playerSpawns[r][3]);
	GivePlayerWeapon(playerid, 24, 7*15);
}

SaveFreeDMStats(playerid)
{
	new line[10];
	format(line, 10, "%d|%d|%d", FreeDM_pInfo[playerid][pKills], FreeDM_pInfo[playerid][pDeaths], FreeDM_pInfo[playerid][pHeadshots]);
	file_Open(FreeDMFile);
	file_SetStr(GetName(playerid), line);
	file_Save(FreeDMFile);
	file_Close();
}
LoadFreeDMStats(playerid)
{
	new line[10];
	file_Open(FreeDMFile);
	if(!file_IsKey(GetName(playerid)))file_SetStr(GetName(playerid), "0|0|0");
	format(line, 10, file_GetStr(GetName(playerid)));
	file_Close();
	sscanf(line, "p<|>ddd", FreeDM_pInfo[playerid][pKills], FreeDM_pInfo[playerid][pDeaths], FreeDM_pInfo[playerid][pHeadshots]);
}

